#include "SerialInterface.h"


SerialInterface::SerialInterface(void)
{
}


SerialInterface::~SerialInterface(void)
{
}

void SerialInterface::Initialize()
{
	// Create a new SerialPort object with default settings.
	_serialPort = gcnew SerialPort();

	// Allow the user to set the appropriate properties.
	_serialPort->PortName = "COM5";
	_serialPort->BaudRate = 9600;
	//_serialPort->Parity = SetPortParity(_serialPort->Parity);
	//_serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
	//_serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
	//_serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);

	// Set the read/write timeouts
	_serialPort->ReadTimeout = 500;
	_serialPort->WriteTimeout = 500;

	_serialPort->Open();
	_continue = true;
	readThread->Start();
}

void SerialInterface::Update()
{
	Read();
}

void SerialInterface::Read()
{
	try
	{
		String^ message = _serialPort->ReadLine();
		Console::WriteLine(message);
	}
	catch (TimeoutException ^) { }
}

void SerialInterface::Close()
{
	readThread->Join();
	_serialPort->Close();
}